import { Component, OnInit } from '@angular/core';
import {Http, Response} from '@angular/http';
import {FormControl} from '@angular/forms';
import {Router} from '@angular/router';
import {Student} from '../../student/share/student.service';

@Component({
  selector: 'app-test01',
  templateUrl: './test01.component.html',
  styleUrls: ['./test01.component.css']
})
export class Test01Component implements OnInit {
  private loading: boolean;
  private data: Object;
  private students: Object[];

  private filterController: FormControl = new FormControl();
  private keyword: string;

  constructor(private http: Http, private router: Router) { }

  ngOnInit() {
    this.filterController.valueChanges
      .debounceTime(500)
      .subscribe(value => {
        this.keyword = value;
        // console.log(this.keyword);
        // console.log(this.filterController.value);
      });
  }

  emit() {
    this.loading = true;
    this.http.request('http://jsonplaceholder.typicode.com/posts/1')
      .subscribe((res: Response) => {
        this.data = res.json();
        console.log(this.data);
        this.loading = false;
      });

  }

  getStudents() {
    this.loading = true;
    this.http.get('/student/findAll')
      .subscribe((res: Response) => {
        this.students = res.json();
        console.log(this.students);
        this.loading = false;
      });
  }

  create() {
    this.router.navigateByUrl('/studentForm/0');
  }

  update(student: Student) {
    // console.log(student);
    this.router.navigateByUrl('/studentForm/' + student.id);
  }

  delete(student: Student) {
    // console.log(student);
    alert('你确定要删除' + student.name + '同学的信息吗？');
  }

}
